home *** CD-ROM | disk | FTP | other *** search
- Path: news.cc.tut.fi!olkikukka!csromu
- From: csromu@olkikukka.uta.fi (Roland Mueller)
- Newsgroups: comp.lang.c++
- Subject: Re: Need Unique ID generation
- Date: 12 Mar 1996 07:02:51 GMT
- Organization: University of Tampere, Finland
- Distribution: world
- Message-ID: <4i37ir$d4f@cc.tut.fi>
- References: <4h3vmq$4a6@alpha.it.net> <31369697.249D@symantec.com> <4h7b4k$837@news.halcyon.com> <4hf6t9$f6n@lib103.its.rpi.edu>
- NNTP-Posting-Host: olkikukka.uta.fi
- X-Newsreader: TIN [version 1.2 PL2]
-
- Barry B Floyd (floydb1@lib103.its.rpi.edu) wrote:
-
- : Has this problem been solved without using a network card ID (i.e.
- : for computers w/o network cards)?
-
- Assuming that you want to provide unique IDs at application level
- you may create a class for ID and hide the constructor as
- private/protected. The only way to get an new ID, i.e.
- an instance of this class, is to call a static function.
-
- Ex.
- template<ID_Type>
- class ID
- {
- public:
- // of course you may also overwrite the new operator,
- // but I forgot the precise syntax for that.
- static ID &New();
-
- private:
- ID(const ID_Type);
- ID_Type id;
- static ID_Type last_id;
- };
-
- template<ID_Type>
- ID<ID_Type>::last_id = 0; // or your init value
-
- template<ID_Type>
- ID<ID_Type> &ID<ID_Type>::New()
- {
-
- return *new (last_id++); // your type must provide ++operator
- }
-
- template<ID_Type>
- ID<ID_Type>::ID(const ID_Type new_id)
- {
- id = new_id;
- }
-
-
-
- --
- Roland Mⁿller KΣrΣjΣt÷rmΣ 4 B 15, 33310 Tampere/Finland
- phone +358-31-3453609
- e-mail Roland.Mueller@uta.fi
-